//
//  RootViewController.m
//  ialone
//
//  Created by lanou on 16/1/11.
//  Copyright © 2016年 JasperSong. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) SCGIFImageView *gifImageView;

// 自定义导航栏
@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIButton *menuButton; // 菜单按钮
@property (nonatomic, strong) UIButton *userInfoButton;  // 用户信息按钮
@property (nonatomic, strong) UILabel *titleLabel;  // 标题标签




// 显示数据部分
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic,assign)NSInteger requestStart;

@property (nonatomic,strong)NSString *pageid;

@property (nonatomic,strong)NSString *create_time;
@property (nonatomic,strong)NSMutableArray *dataAll;
@property (nonatomic,strong)DDLoginViewController *LoginView;
@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _dataAll = [NSMutableArray array];
    _dataArr = [NSMutableArray array];
    _requestStart = 1;
    _pageid = @"0";
    _create_time = @"0";
    // 隐藏 导航栏
    self.navigationController.navigationBarHidden = YES;
    
     self.view.backgroundColor = [UIColor cyanColor];
    
    // 加载子视图
    [self addSubViews];
    
    
    
    
    
    _leftVC = [[DDLeftViewController alloc]init];
    [self addChildViewController:_leftVC];
    _leftVC.view.frame = CGRectMake(- kScreenWidth, 0, kScreenWidth, kScreenHeight);
    [self.view addSubview:_leftVC.view];
    
    _rightVC = [[DDRightViewController alloc]init];
    [self addChildViewController:_rightVC];
    _rightVC.view.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight);
    [self.view addSubview:_rightVC.view];
    
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        _requestStart = 1;
        _create_time = @"0";
        _pageid = @"0";
        [self setUpData];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        _requestStart += 1 ;
        DDRootModel *model = _dataArr[_dataArr.count - 1] ;
        _create_time = model.create_time;
        _pageid = model.pageid;
        [self setUpData];
        // 更改start
        
    }];
    
    [self hudShow];
    
    // 加载数据
    [self setUpData];
}

- (void)addSubViews{
    // 初始化表格
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:(UITableViewStylePlain)];
    // 保证上面不会有空格
    self.automaticallyAdjustsScrollViewInsets = NO;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    // 行高整页
    _tableView.rowHeight = kScreenHeight;
    // 点击顶部导航栏到最上面
    _tableView.scrollsToTop = YES;
    // 整页翻动
    _tableView.pagingEnabled = YES;
    [self.view addSubview:_tableView];
    
    
    // 初始化 自定义顶部栏 高度与标准的UINavigationController相同 44
    _topView = [[UIView alloc]initWithFrame:CGRectMake(0, 20, kScreenWidth, 44)];
    [self.view addSubview:_topView];
    
    _menuButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _menuButton.frame = CGRectMake(kLMarge, 0, 4 * kLMarge,_topView.height );
    [_menuButton addTarget:self action:@selector(menuButton:) forControlEvents:(UIControlEventTouchUpInside)];
    [_menuButton setImage:[UIImage imageNamed:@"menu"] forState:(UIControlStateNormal)];
    [_topView addSubview:_menuButton];
    
    _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth/2 - 5*kLMarge, kLMarge, 10*kLMarge, _topView.height - 2 * kLMarge)];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.text = @"单独";
    _titleLabel.textColor = [UIColor whiteColor];
    [_topView addSubview:_titleLabel];
    
    _userInfoButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _userInfoButton.frame = CGRectMake(kScreenWidth - kLMarge - 44, _menuButton.top,44, _menuButton.height);
    [_userInfoButton setImage:[UIImage imageNamed:@"userInfo"] forState:(UIControlStateNormal)];
    [_userInfoButton addTarget:self action:@selector(userInfo:) forControlEvents:(UIControlEventTouchUpInside)];
    [_topView addSubview:_userInfoButton];
}


- (void)setUpData{
    NSString *str =[NSString stringWithFormat:@"http://static.owspace.com/index.php?m=Home&c=Api&a=getList&p=%ld&client=iphone&page_id=%@&create_time=%@&device_id=B709CBC5-0E2F-4C89-9E24-6AFBBA86C58C&iOS_version=1.0.0",_requestStart,_pageid,_create_time];
    [PKRequestManager requestWithUrl:str parmDic:nil method:GET succeedWithData:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *arr = dic[@"datas"];
        if (_requestStart == 1) {
            [_dataArr removeAllObjects];
        }
        
        [self hudHide];
        
        for (NSDictionary *one in arr) {
            DDRootModel *model = [[DDRootModel alloc]init];
            [model setValuesForKeysWithDictionary:one];
            [_dataArr addObject:model];
            [_dataAll addObject:model];
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [_tableView reloadData];
    } failedWithError:^(NSError *connectionError) {
        
    }];
}


#pragma mark ----- 表格的代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DDRootviewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainPage"];
    if (cell == nil){
        cell = [[DDRootviewTableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"MainPage"];
    }else{
        // 消失于屏幕就会进入重用池  这样就走这个方法
        [cell repair];
    }
    
//    CGRect rectInTableView = [tableView rectForRowAtIndexPath:indexPath];
//    CGRect rectInSuperview = [tableView convertRect:rectInTableView toView:[tableView superview]];
    
    
    
    DDRootModel *model = _dataArr[indexPath.row];
    cell.model = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}
#pragma mark ----- 按钮的响应
- (void)menuButton:(UIButton *)butt{
    
    [UIView animateWithDuration:1 animations:^{
       _leftVC.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    } completion:^(BOOL finished) {
        for (int i = 0; i < 2; i ++){
            UIButton *button = (UIButton *)[self.view viewWithTag:3400 + i];
            button.hidden = NO;
            [UIView animateWithDuration:0.3 animations:^{
                button.transform = CGAffineTransformScale(button.transform, 10, 10);
            }];
        }
        
        UILabel *label2 = (UILabel *)[self.view viewWithTag:3301];
        label2.hidden = NO;
        [UIView animateWithDuration:0.3 animations:^{
            label2.transform = CGAffineTransformScale(label2.transform, 10, 1);
        }];
    }];
    
    UILabel *label = (UILabel *)[self.view viewWithTag:3300];
    for (int i = 0; i < 6; i++){
        UIButton *button = (UIButton *)[self.view viewWithTag:2700 + i];
        [UIView animateWithDuration:1.8 - 0.3 * i  animations:^{
            button.frame = CGRectMake((kScreenWidth - 135) / 2, label.bottom + 40 + 65 * i, 135, 45);
        }];
    }

  
    
}
- (void)userInfo:(UIButton *)button{
    if ([[DDManager sharedHandle] islogin] == YES) {
            UIButton *button2 = (UIButton *)[self.view viewWithTag:1250];
            [UIView animateWithDuration:1.2   animations:^{
                button2.frame = CGRectMake(kScreenWidth/3, kScreenHeight/2+30, kScreenWidth/3, 50);
            }];
        
            [UIView animateWithDuration:1 animations:^{
                _rightVC.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        
        
            } completion:^(BOOL finished) {
                for (int i = 0; i < 2; i ++){
                    UIButton *button = (UIButton *)[self.view viewWithTag:7890 + i];
                    button.hidden = NO;
                    [UIView animateWithDuration:0.3 animations:^{
                        button.transform = CGAffineTransformScale(button.transform, 10, 10);
                    }];
                }
                
                
            } ];
    }else
    {
    _LoginView = [[DDLoginViewController alloc]init];
    [self.navigationController pushViewController:_LoginView animated:YES];
    }
}

- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    DDSoundDetailsViewController *sdVC = [[DDSoundDetailsViewController alloc]init];
    
    DDRootModel *model = _dataArr[indexPath.row];
    
    sdVC.html5 = model.html5;
    
    sdVC.pageid = model.pageid;
    
    sdVC.category = model.category;
    
    sdVC.author = model.author;
    
    sdVC.titlename = model.title;
    
    sdVC.thumbnail = model.thumbnail;
    
    [self.navigationController pushViewController:sdVC animated:YES];
    
}



- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)hudShow{
    [self p_setupProgressHud];
}

- (void)hudHide{
    [_hud hide:YES];
}

- (void)p_setupProgressHud
{
    self.hud = [[MBProgressHUD alloc] initWithView:self.view];
    
        // 更改指示器的样式
    
        NSString* filePath = [[NSBundle mainBundle] pathForResource:@"load.gif" ofType:nil];
        NSData* imageData = [NSData dataWithContentsOfFile:filePath];
    
        _gifImageView = [[SCGIFImageView alloc] init];
    //    _gifImageView.center = CGPointMake(kScreenWidth/2, kScreenHeight/2);
        [_gifImageView setData:imageData];
    
    
        self.hud.customView = [[UIImageView alloc] initWithImage:_gifImageView.image];
    
    _hud.frame = self.view.bounds;
    _hud.minSize = CGSizeMake(100, 100);
    _hud.mode = MBProgressHUDModeIndeterminate;
    [self.view addSubview:_hud];
    [_hud show:YES];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
